package com.shchuangzhi.smartguard.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.elvishew.xlog.XLog;
import com.shchuangzhi.smartguard.R;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.Job;
import com.shchuangzhi.smartguard.core.JobManager;
import com.shchuangzhi.smartguard.core.Machine;
import com.shchuangzhi.smartguard.core.Notify;
import com.shchuangzhi.smartguard.core.NotifyMessage;
import com.shchuangzhi.smartguard.core.command.Command;

/**
 * A simple {@link Fragment} subclass.
 */
public class StatusBar extends Fragment {

    private Handler handler;
    private TextView tvCurrentTime;
    private TextView tvHostStatus;
    private TextView tvNetworkStatus;
    private TextView tvNotifyInfo;

    public StatusBar() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_status_bar, container, false);
        tvCurrentTime = (TextView)view.findViewById(R.id.status_bar_current_time);
        tvHostStatus = (TextView)view.findViewById(R.id.status_bar_host_status);
        tvNetworkStatus = (TextView)view.findViewById(R.id.status_bar_network_status);
        tvNotifyInfo = (TextView)view.findViewById(R.id.status_bar_info);

        handler = new Handler() {
            public void handleMessage(Message msg) {
                try {
                    switch(msg.what) {
                        case Consts.JOB_CURRENT_TIME_PRINT: tvCurrentTime.setText((String)msg.obj);
                            break;
                        case Consts.JOB_DATA_SYNC:case Consts.JOB_RECEIVER_DATA_SYNC: {
                            Command.CommandResult result = (Command.CommandResult)msg.obj;
                            if (result.OK()) {
                                tvHostStatus.setTextColor(Color.GREEN);
                                tvHostStatus.setText(Consts.HOST_OK);
                            } else {
                                tvHostStatus.setTextColor(Color.RED);
                                tvHostStatus.setText(result.Message());
                            }
                        }
                        break;
                        case Consts.JOB_NETWORK_CHECK:{
                            //boolean good = (boolean)msg.obj;
                            Command.CommandResult result = (Command.CommandResult)msg.obj;
                            if (result.OK()) {
                                tvNetworkStatus.setTextColor(Color.GREEN);
                                tvNetworkStatus.setText(Consts.NETWORK_OK);
                            } else {
                                tvNetworkStatus.setTextColor(Color.RED);
                                tvNetworkStatus.setText(Consts.NETWORK_FAIL);
                            }
                        }
                        break;
                        case Consts.NOTIFY_DATA_SYNC:case Consts.NOTIFY_RECEIVER_DATA_SYNC: {
                            NotifyMessage message = (NotifyMessage)msg.obj;
                            if (message.getLevel().equals(NotifyMessage.LEVEL_INFO)) {
                                tvNotifyInfo.setText(message.getMessage());
                            }
                        }
                        break;
                        default:break;
                    }
                } catch(Exception e) {
                    XLog.e("", e);
                }
            }
        };

        final Job printJob = JobManager.get(Consts.JOB_CURRENT_TIME_PRINT);
        if (printJob != null) {
            printJob.addCallback(Consts.JOB_CURRENT_TIME_PRINT, new Job.JobCallback() {
                @Override
                public void Callback(Object result) {
                    handler.sendMessage(handler.obtainMessage(printJob.getName(), result));
                }
            });
        }

        if (Machine.getInstance().getType() == Machine.MachineType.Gate) {
            final Job dataSyncJob = JobManager.get(Consts.JOB_DATA_SYNC);
            if (dataSyncJob != null) {
                dataSyncJob.addCallback(Consts.JOB_DATA_SYNC, new Job.JobCallback() {
                    @Override
                    public void Callback(Object result) {
                        handler.sendMessage(handler.obtainMessage(dataSyncJob.getName(), result));
                    }
                });
                dataSyncJob.addNotify(Consts.NOTIFY_DATA_SYNC, new Notify() {
                    @Override
                    public void notify(NotifyMessage message) {
                        handler.sendMessage(handler.obtainMessage(Consts.NOTIFY_DATA_SYNC, message));
                    }
                });
            }
        } else {
            final Job ReceiverDataSyncJob = JobManager.get(Consts.JOB_RECEIVER_DATA_SYNC);
            if (ReceiverDataSyncJob != null) {
                ReceiverDataSyncJob.addCallback(Consts.JOB_RECEIVER_DATA_SYNC, new Job.JobCallback() {
                    @Override
                    public void Callback(Object result) {
                        handler.sendMessage(handler.obtainMessage(ReceiverDataSyncJob.getName(), result));
                    }
                });
                ReceiverDataSyncJob.addNotify(Consts.NOTIFY_RECEIVER_DATA_SYNC, new Notify() {
                    @Override
                    public void notify(NotifyMessage message) {
                        handler.sendMessage(handler.obtainMessage(Consts.NOTIFY_RECEIVER_DATA_SYNC, message));
                    }
                });
            }
        }

        final Job networkCheckJob = JobManager.get(Consts.JOB_NETWORK_CHECK);
        if (networkCheckJob != null) {
            networkCheckJob.addCallback(Consts.JOB_NETWORK_CHECK, new Job.JobCallback() {
                @Override
                public void Callback(Object result) {
                    handler.sendMessage(handler.obtainMessage(networkCheckJob.getName(), result));
                }
            });
        }

        return view;
    }
}
